<template>
	<view class="dormitory_set_box">
		<Tips/>
		<MyCard :bgc="primary_hot_color">
			<view class="header_box">
				<view class="cover" v-if="dormitoryData?.dormitory.dormitory_cover_url">
					<Avatar 
						:joinUrlFun="joinDormitoryCover" 
						:fileName="dormitoryData?.dormitory.dormitory_cover_url"
						size="150rpx"
						radius="150rpx"
						/>
				</view>
				<view class="info_box">
					<view class="dormitory_name text_ellipsis">
						{{dormitoryData?.dormitory.dormitory_name}}
					</view>
					<view class="admin text_ellipsis">
						管理员: {{dormitoryData?.admin.nick_name_by_dormitory}}
					</view>
				</view>
			</view>
		</MyCard>
		<MyCard>
			<view class="card">
				<MyItem
					name="宿舍头像" 
					iconFileName="setAvatar.png"
					iconBgc="#ED655F"
					:clickFun="uploadImg"/>
				<MyItem
					name="宿舍名字"
					iconFileName="setDormitoryName.png"
					iconBgc="#F4AF69"
					:clickFun="openDormitoryNameModalFun"/>
				<MyItem 
					name="宿舍介绍"
					iconFileName="dormitoryInfo.png"
					iconBgc="#427EC0"
					:clickFun="openDescriptModalFun"/>
				<MyItem 
					name="管理员转让"
					iconFileName="user_give.png"
					iconBgc="#F06090"
					:clickFun="chooseAdminModelFun"/>
			</view>
		</MyCard>
		<MyCard>
			<view class="card">
				<MyItem
					name="接收所有短信消息" 
					iconFileName="shortMessage.png"
					iconBgc="#585187"
					:clickFun="setIsOpenPushFun">
					<template #rightBtn>
						<view>
							<MySwitch :state="isOpenPush"/>
						</view>
					</template>
				</MyItem>
				<MyItem
					name="值日暂停" 
					iconFileName="pauseDuty.png"
					iconBgc="#31887B"
					:clickFun="isSetIsPauseDutyFun">
					<template #rightBtn>
						<view>
							<MySwitch :state="isPauseDuty"/>
						</view>
					</template>
				</MyItem>
				<MyItem
					:name="weekType===WeekType.SIGLE ? '本周为单周' : '本周为双周'" 
					iconFileName="weekType.png"
					iconBgc="#0085A6"
					:clickFun="isSetWeekTypeFun">
					<template #rightBtn>
						<view>
							<MySwitch :pointTip="['换','换']" :state="weekType===WeekType.SIGLE ? true : false"/>
						</view>
					</template>
				</MyItem>
			</view>
		</MyCard>
		<MyCard>
			<MyItem
				name="我在该宿舍里的昵称" 
				iconFileName="setNickName.png"
				iconBgc="#65BFD1"
				:clickFun="openNickNameModalFun"/>
		</MyCard>
		<view class="footer">
			<MyButton :clickFun="isQuitDormitoryFun" :bgc="primary_hot_color">
				{{showKickDormitory(dormitoryData)}}
			</MyButton>
			<view class="time" v-if="isDeleteingDormitory">
				宿舍将在{{deleteDormitoryJobInfo?.cronOrTime}}解散
			</view>
		</view>
		<MyModal :openModalFlag="modalNickNameFlag" :okFun="submitNickName">
			<template #title>
				修改自己在该宿舍的昵称
			</template>
			<template #content>
				<SetNickNameByDormitory 
					:dormitoryAllId="dormitoryAllId" 
					:userInfo="dormitoryData?.user" 
					@setNickNameOk="init"
					:clickSubmitNickNameFlag="clickSubmitNickNameFlag"
					v-if="dormitoryData?.user"/>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modalDescriptFlag" :okFun="submitDescript">
			<template #title>
				修改宿舍描述
			</template>
			<template #content>
				<SetDormitoryDescript 
					:clickSubmitDescriptFlag="clickSubmitDescriptFlag"
					:dormitoryAllId="dormitoryAllId" 
					@setDescriptOk="init"
					:dormitory="dormitoryData?.dormitory"
					v-if="dormitoryData?.dormitory"/>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modelIsSetIsPauseDuty" :okFun="setIsPauseDuty">
			<template #title>
				确定要暂停值日吗
			</template>
			<template #content>
				<TipInfo :color="shallow_text_color" id="12"></TipInfo>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modelIsSetWeekTypeFlag" :okFun="setWeekTypeFun">
			<template #title>
				确定要调平{{weekType===WeekType.SIGLE ? '本周为双周' : '本周为单周'}}吗
			</template>
			<template #content>
				<TipInfo :color="shallow_text_color" id="17"></TipInfo>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modalDormitoryNameFlag" :okFun="submitDormitoryName">
			<template #title>
				修改宿舍名字
			</template>
			<template #content>
				<SetDormitoryName 
					:clickSubmitDormitoryNameFlag="clickSubmitDormitoryNameFlag"
					:dormitoryAllId="dormitoryAllId" 
					@setDormitoryNameOk="init"
					:dormitory="dormitoryData?.dormitory"
					v-if="dormitoryData?.dormitory"/>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modelChooseIsAdminFlag" :okFun="isOkGiveDormitory">
			<template #title>
				管理员转让
			</template>
			<template #content>
				<view class="choose_name_box">
					确定转让管理员给
					<text class="choose_name">
						{{chooseAdminUser?.nick_name_by_dormitory}}
					</text>
					吗？
				</view>
			</template>
		</MyModal>
		<MyModal :openModalFlag="modalIsQuitDormitoryFlag" :okFun="quitDormitoryFun">
			<template #title>
				确认{{showKickDormitory(dormitoryData)}}吗
			</template>
			<template #content>
				<TipInfo v-if="!isDeleteingDormitory && dormitoryData?.admin.user_id===dormitoryData?.user.user_id" :color="shallow_text_color" id="9"></TipInfo>
			</template>
		</MyModal>
		<uni-popup ref="chooseAdminRef" background-color="#fff" >
			<view class="popup_content">
				<view class="title">
					{{allJoinerData.length ? '选择转让的目标室友' : '宿舍只有1人,无法转让。'}}
				</view>
				<viwe class="choose_box">
					<view 
						@click="chooseIsGiveAdminFun(userItem)" 
						v-for="userItem in allJoinerData" 
						:key="userItem.user_id" 
						class="item_user text_ellipsis"
					>
						<viwe>
							<Avatar :fileName="userItem.user_avatar" size="70rpx" />
						</viwe>
						<view class="user_name text_ellipsis">{{userItem.nick_name_by_dormitory}}</view>
					</view>
				</viwe>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		onShow
	} from '@dcloudio/uni-app'
	import {ref,onDeactivated, nextTick} from 'vue'
	
	import MyItem from '@/components/MyItem/MyItem.vue'
	import Tips from '@/components/Tips/Tips.vue'
	import MyCard from '@/components/MyCard/MyCard.vue'
	import Avatar from '@/components/Avatar/Avatar.vue'
	import MySwitch from '@/components/MySwitch/MySwitch.vue'
	import MyModal from '@/components/MyModal/MyModal.vue'
	import SetNickNameByDormitory from '@/components/Dormitory/SetNickNameByDormitory/SetNickNameByDormitory.vue'
	import SetDormitoryDescript from '@/components/Dormitory/SetDormitoryDescript/SetDormitoryDescript.vue'
	import SetDormitoryName from '@/components/Dormitory/SetDomitoryName/SetDomitoryName.vue'
	import MyButton from '@/components/MyButton/MyButton.vue'
	import TipInfo from '@/components/TipInfo/TipInfo.vue'
	
	import {dynamicStyleStore} from '@/stores/dynamicStyle.js'
	
	import {
		joinDormitoryCover
	} from '@/utils/imgJoinName.js'
	
	import {
		DormitoryModel
	} from '@/service/model/dormitory.js'
	
	import {
		IsDormitoryPush,
		IsPauseDuty,
		WeekType
	} from '@/enum/GlobalEnum.js'
	
	import {uploadImgHook} from '@/hooks/uploadImg.js'
	import {verifyIsLogin,verifyIsDormitoryAdmin} from '@/hooks/verify.js'
	
	const dormitoryModel = new DormitoryModel()
	
	const dynamicStyle_store=dynamicStyleStore()
	
	const {
		primary_main_bgc,
		primary_cool_color,
		primary_hot_color,
		shallow_text_color,
		primary_text_color
	} = dynamicStyle_store.globalStyle
	
	const dormitoryData=ref()
	const dormitoryAllId=uni.getStorageSync('homeData')
	const deleteDormitoryJobInfo=ref()
	const isOpenPush = ref(true)
	const isPauseDuty = ref(0)
	const weekType = ref(0)
	const chooseAdminUser=ref()
	const chooseAdminRef=ref()
	const allJoinerData=ref([])
	const modalNickNameFlag = ref(0)
	const modelChooseIsAdminFlag=ref(0)
	const modelIsSetWeekTypeFlag = ref(0)
	const modalDescriptFlag = ref(0)
	const modalDormitoryNameFlag = ref(0)
	const modalIsQuitDormitoryFlag=ref(0)
	const clickSubmitNickNameFlag = ref(0)
	const clickSubmitDescriptFlag = ref(0)
	const clickSubmitDormitoryNameFlag = ref(0)
	const isDeleteingDormitory = ref(false)
	const modelIsSetIsPauseDuty = ref(0)
	
	//设置是否接收所有推送消息
	const setIsOpenPushFun=async()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		//这里isOpenPush要取反
		const {data,success} = await dormitoryModel.updateDormitoryIsPush({
			state:isOpenPush.value ? 0 : 1
		},dormitoryAllId.dormitory_id)
		if(success){
			uni.$emit('tips',data,'success',isOpenPush.value ? 3000 : 3000)
			init()
		}
	}
	//切换宿舍单双周
	const setWeekTypeFun=async()=>{
		const {data,success} = await dormitoryModel.patchDormitoryWeekType({
			weekType:weekType.value===0 ? 1 : 0
		},dormitoryAllId.dormitory_id)
		
		if(success){
			uni.$emit('tips',data,'success',4000)
			modelIsSetWeekTypeFlag.value = -Math.random()
			init()
		}
	}
	//确认调平单双周
	const isSetWeekTypeFun=()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		modelIsSetWeekTypeFlag.value = Math.random()
	}
	//确认设置宿舍暂定操作吗
	const isSetIsPauseDutyFun=()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		isPauseDuty.value===IsPauseDuty.YES ? setIsPauseDuty() : (modelIsSetIsPauseDuty.value=Math.random())
	}
	//设置是否暂停值日
	const setIsPauseDuty=async()=>{
		const {data,success} = await dormitoryModel.patchIsPauseDuty({},dormitoryAllId.dormitory_id)
		if(success){
			uni.$emit('tips',data,'success',2000)
			init()
		}
	}
	//封面上传
	const uploadImg=()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		uploadImgHook("uploadDormitoryCover",`/upload/dormitory/cover/${dormitoryData.value.dormitory.id}`)
	}
	
	//监听图片上传成功
	uni.$on('uploadDormitoryCover',function(data){
		;(()=>{
			if(data){
				init()
			}
		})()
	})
	
	//确认转让吗
	const chooseIsGiveAdminFun=(data)=>{
		chooseAdminRef.value.close()
		chooseAdminUser.value = data
		modelChooseIsAdminFlag.value = Math.random()
	}
	
	//转让宿舍管理员
	const isOkGiveDormitory=async()=>{
		const {data,success} = await dormitoryModel.updateDormitoryAdmin({user_id:chooseAdminUser.value.user_id},dormitoryAllId.dormitory_id)
		if(success){
			init()
			modelChooseIsAdminFlag.value = -Math.random()
			uni.$emit('tips',data,'success')
		}
	}
	
	//获取宿舍解散任务信息
	const getDeleteDormitoryFun=async()=>{
		const {data,success} = await dormitoryModel.getDeleteDormitory({},dormitoryAllId.dormitory_id)
		if(success){
			deleteDormitoryJobInfo.value = data
		}
	}
	
	//打开转让管理员的弹窗
	const chooseAdminModelFun=async()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		chooseAdminRef.value.open('right')
		let {
			data,
			success
		} = await dormitoryModel.getAllJoiner({
			limit:1000,
			page: 0,
			orderBy: 'createAt.asc'
		}, dormitoryAllId.dormitory_id)
		if(success){
			data = data.filter(item=>item.user_id !== dormitoryData.value.admin.user_id)
			allJoinerData.value = data
		}
	}
	
	//主动退出宿舍
	const quitDormitoryFun=async()=>{
		const isQuitDormitory = showKickDormitory(dormitoryData.value) === "退出宿舍"
		const {data,success} = isQuitDormitory
		? await dormitoryModel.delteQuitDormitory({},dormitoryAllId.relation_id)
		: (
			showKickDormitory(dormitoryData.value) === "解散宿舍" 
			? await dormitoryModel.deleteDormitory({},dormitoryAllId.dormitory_id)
			: await dormitoryModel.patchUnDeleteDormitory({},dormitoryAllId.dormitory_id)
		)
		if(success){
			if(isQuitDormitory){
				uni.removeStorageSync('homeData')
				uni.reLaunch({
					url:'/pages/Dormitorys/Dormitorys',
					success:()=>uni.$emit('tips',data,'success')
				})				
			}else{
				init()
				uni.$emit('tips',data,'success')
			}
		}
	}
	
	//展示删除退出提示
	const showKickDormitory=(_dormitoryData)=>{
		return _dormitoryData?.admin.user_id === _dormitoryData?.user.user_id 
			? (
				_dormitoryData?.dormitory.state === 3
				? "取消解散"
				: "解散宿舍"
			)
			: '退出宿舍'
	}
	
	//是否确认退出宿舍
	const isQuitDormitoryFun=()=>{
		modalIsQuitDormitoryFlag.value = Math.random()
	}
	
	//打开修改昵称弹窗
	const openNickNameModalFun=()=>{
		verifyIsLogin()
		modalNickNameFlag.value=Math.random()
	}
	//打开修改描述弹窗
	const openDescriptModalFun=()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		modalDescriptFlag.value=Math.random()
	}
	//打开修改宿舍名字的弹窗
	const openDormitoryNameModalFun=()=>{
		verifyIsLogin()
		verifyIsDormitoryAdmin(dormitoryData.value.admin,dormitoryData.value.user)
		modalDormitoryNameFlag.value=Math.random()
	}
	
	//点击提交修改昵称
	const submitNickName=()=>{
		clickSubmitNickNameFlag.value = Math.random()
	}
	//点击提交修改介绍
	const submitDescript=()=>{
		clickSubmitDescriptFlag.value = Math.random()
	}
	//点击提交修改宿舍名字
	const submitDormitoryName=()=>{
		clickSubmitDormitoryNameFlag.value = Math.random()
	}
	
	const init=async()=>{
		const {relation_id,dormitory_id} = dormitoryAllId
		const {data,success} = await dormitoryModel.getDormitoryInfo({},relation_id,dormitory_id)
		if(success){
			dormitoryData.value = data
			isOpenPush.value = data.dormitory.isPush === IsDormitoryPush.YES ? true : false
			isPauseDuty.value = data.dormitory.isPauseDuty === IsPauseDuty.YES ? 1 : 0
			weekType.value = data.dormitory.dormitory_weekType
			if(data.dormitory.state === 3){
				isDeleteingDormitory.value = true
				nextTick(()=>{
					getDeleteDormitoryFun()
				})
			}else{
				isDeleteingDormitory.value = false
			}
		}
	}
	
	onShow(()=>{
		init()
		nextTick(()=>{
			uni.$emit('autoShowPushScreenAd')
		})
	})
	onDeactivated(()=>{
		uni.$off('uploadDormitoryCover')
	})
</script>

<style lang="less">
	.text_ellipsis{
		white-space:nowrap;
		text-overflow:ellipsis;
		overflow: hidden;
	}
	.dormitory_set_box{
		height: calc(100vh);
		padding: 1rpx 0;
		background-color: v-bind(primary_main_bgc);
		padding-bottom: 80rpx;
		.choose_name_box{
			font-size: 30rpx;
			color: v-bind(primary_text_color);
			.choose_name{
				color: v-bind(primary_hot_color);
			}
		}
		.popup_content {
			padding: 20rpx;
			background-color: #fff;
			width: 400rpx;
			color: v-bind(primary_text_color);
			box-sizing: border-box;
			.title{
				width: 100%;
				margin-bottom: 20rpx;
				font-size: 30rpx;
				display: flex;
			}
			.choose_box{
				display: grid;
				grid-template-columns: 1fr 1fr;
				gap: 8rpx;
				width: 100%;
				.item_user{
					display: flex;
					justify-content: center;
					align-items: center;
					flex-wrap: wrap;
					padding: 10rpx 5rpx;
					border: 2rpx solid v-bind(primary_text_color);
					border-radius: 10rpx;
					.user_name{
						text-align: center;
						font-size: 24rpx;
						margin-top: 5rpx;
						padding: 0 4rpx;
						width: 100%;
					}
				}
			}
		}
		.card{
			margin: 10rpx 0;
		}
		.header_box{
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			.info_box{
				width: 75%;
				display: flex;
				flex-wrap: wrap;
				margin-left: 20rpx;
				color: #FFFFFF;
				.dormitory_name{
					width: 100%;
					font-size: 35rpx;
				}
				.admin{
					width: 100%;
					font-size: 25rpx;
				}
			}
		}
		.footer{
			padding: 0 20rpx;
			padding-bottom: 80rpx;
			.time{
				display: flex;
				justify-content: center;
				margin-top: 10rpx;
				color: v-bind(primary_hot_color);
				font-size: 30rpx;
				width: 100%;
			}
		}
	}
</style>
